package com.licq.hongmeng.net;


import com.licq.hongmeng.bean.BaseBean;
import ohos.app.Context;
import retrofit2.Call;
import retrofit2.Response;


/**
 * 继承方式
 *
 * @param <T>
 */
public abstract class NetInheritObserver<T extends BaseBean> extends RxNetObserver<T> implements NetCallBack<T> {

    public NetInheritObserver(Context context) {
        super(context);
    }


    @Override
    public void onSubscribe(Call<T> call) {
        try {
            onSubscribed(call);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable throwable) {
        error(throwable);
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        T info = response.body();
        if (info == null) {
            try {
                onFail("onNext method parameter is empty");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        if (info.getCode() == RESPONSE_CODE_OK) {
            try {
                onSuccess(info, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //对服务器返回的通用问题的统一拦截处理
//            if (con != null && con.get() != null) {
//                DeviceInfo.analysisCode(con.get(), info.getCode(), info.getErrorMsg());
//            }
//            try {
//                onFail(info.getCode(), info.getErrorMsg(),null);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }
    }
}
